package com.oc.mba.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.oc.mba.controller.BackendController;
import com.oc.mba.dao.BaseDAOHibernate;
import com.oc.mba.dao.OcPaperDAO;
import com.oc.mba.entity.OcPaper;
import com.oc.mba.util.CommLog;

@Repository
public class OcPaperDAOImpl extends BaseDAOHibernate implements OcPaperDAO {
	
	private static final CommLog logger = new CommLog(OcPaperDAOImpl.class);
	
	@Override
	public List<OcPaper> list() {
		return findByProperty("paperStatus", 1);
	}

	public List<OcPaper> findByProperty(String propertyName, Object value) {
		logger.debug("finding OcPaper instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from OcPaper as model where model." + propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		}
		catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public OcPaper findPaper(Integer paperID) {
		List<OcPaper> papers = findByProperty("id", paperID);
		if (papers != null && papers.size() > 0) {
			return papers.get(0);
		}
		return null;
	}

	@Override
	public List<OcPaper> listAll() {
		logger.debug("finding OcPaper list");
		try {
			String queryString = "from OcPaper as model ";
			return getHibernateTemplate().find(queryString, null);
		}
		catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public void save(OcPaper paper) {
		getHibernateTemplate().save(paper);
	}

	@Override
	public void deletePaper(Integer paperID) {
		OcPaper paper = findPaper(paperID);
		if (paper != null) {
			getHibernateTemplate().delete(paper);
		}
	}

	@Override
	public void update(OcPaper paper) {
		getHibernateTemplate().update(paper);
	}
}
